public class Test {
    //给定一个有序整型数组, 实现二分查找
    public static int binarySearch(int [] arr,int n) {
        int left = 0;
        int right = arr.length-1;
        while(left <= right) {
            int mid = (left+right)/2;
            if(arr[mid] > n){
                right = mid-1;
            }else if(arr[mid] < n){
                left = mid+1;
            }else{
                return mid;
            }

        }
        return -1;

    }

    public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6,7,8,9,10};
        int ret = binarySearch(array,8);
        System.out.println(ret);

    }
}
